﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using hg3.hg3;

namespace Baocao
{
    public partial class ChangeUserInfo : System.Web.UI.Page
    {
        private SqlConnection appProxy;
        private DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["appProxy"] != null && Session["UserInfo"] != null)
            {
                appProxy = (SqlConnection)Session["appProxy"];
                dt = Session["UserInfo"] as DataTable;
                if (!IsPostBack)
                {
                    ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("FullName")).Text = dt.Rows[0]["UserName"].ToString();
                    ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Company")).Text = dt.Rows[0]["Company"].ToString();
                    ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Phone")).Text = dt.Rows[0]["Phone"].ToString();
                    ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Email")).Text = dt.Rows[0]["Email"].ToString();
                    ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Address")).Text = dt.Rows[0]["Address"].ToString();
                    if (dt.Rows[0]["UserID"].ToString().ToUpper().Equals("HIVI"))
                    {
                        ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("FullName")).ReadOnly = true;
                        ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Company")).ReadOnly = true;
                        ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Phone")).ReadOnly = true;
                        ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Email")).ReadOnly = true;
                        ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Address")).ReadOnly = true;
                        ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("ConfirmNewPassword")).ReadOnly = true;
                        ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("CurrentPassword")).ReadOnly = true;
                        ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("NewPassword")).ReadOnly = true;
                        ((Button)ChangePassword1.ChangePasswordTemplateContainer.FindControl("ChangePasswordPushButton")).Enabled = false;
                        //((Button)ChangePassword1.ChangePasswordTemplateContainer.FindControl("CancelPushButton")).Enabled = false;
                    }
                }
            }
            else
            {
                Response.Redirect("~\\Login.aspx");
            }

        }

        protected void ChangePasswordPushButton_Click(object sender, EventArgs e)
        {
            string userid = dt.Rows[0]["UserID"].ToString();
            string pass = ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("CurrentPassword")).Text;
            string newpass = ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("NewPassword")).Text;
            string username = ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("FullName")).Text;
            string company = ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Company")).Text;
            string phone = ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Phone")).Text;
            string email = ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Email")).Text;
            string add = ((TextBox)ChangePassword1.ChangePasswordTemplateContainer.FindControl("Address")).Text;
            if (pass != dt.Rows[0]["Password"].ToString())
            {
                ChangePassword1.ChangePasswordFailureText = "Mật khẩu cũ nhập sai!";
                return;
            }
            else
            {
                ChangePassword1.ChangePasswordFailureText = "";
                try
                {
                    sql.SQLExecute(ref appProxy, "UPDATE UserInfo SET Password='" + newpass + "',UserName=N'" + username + "',Company = N'" + company + "',Phone='" + phone + "',Email='" + email + "',Address= N'" + add + "' WHERE UserID='" + userid + "'", 30);
                }
                catch (Exception ex)
                {
                    ChangePassword1.ChangePasswordFailureText = "Cập nhật không thành công";
                    return;
                }
                ChangePassword1.ChangePasswordFailureText = "Cập nhật thành công!";
                DataSet ds = new DataSet();
                sql.SQLRetrieve(appProxy, "SELECT * FROM userinfo WHERE UPPER(UserID) = '" + userid.ToUpper() + "' AND UPPER(Password) = '" + newpass.ToUpper() + "'", "tbluserinfo", ref ds);
                Session["UserInfo"] = ds.Tables["tbluserinfo"];
                Response.Redirect("~\\Invoice.aspx");
            }
        }
    }
}
